package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.system.ICoreDataTagDao;
import yl.hs.bmipfull.pojo.system.RoleInfoEntity;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.service.system.ISiteMessageMainService;
import yl.hs.bmipfull.service.system.ISiteMessageReceiptService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.system.MsgModel;

import java.util.stream.Collectors;

/**
 * 作用:  站内信息收件信息控制层<br/>
 * 创建时间: 2021年05月12日 16时32分18秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "站内信息收件信息Web接口", tags = "系统管理——站内信息收件信息操作")
@RequestMapping(value = "/api/message/receipt")
@RestController
public class SiteMessageReceiptController extends ControllerBase {
    @Autowired
    ISiteMessageReceiptService service;
    @Autowired
    ISearchViewService svSVC;
    @Autowired
    ICoreDataTagDao coreDataTagDao;
    @Autowired
    ISiteMessageMainService mainService;

    @ApiOperation(value = "查询站内信息收件信息", notes = "查询所有站内信息收件信息")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public WebResult queryAll() {
        return success("查询成功", service.queryAll());
    }

    @ApiOperation(value = "查询站内信息收件信息", notes = "分页查询站内信息收件信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var user = HsApp.getCurrent();
            var tags = coreDataTagDao.queryBySN(user.getUserSN().split(","));
            var roleCode = Tl.getDBMapCode(RoleInfoEntity.class);
            var roles = tags.stream()
                    .filter(p -> p.getTagType().equals(roleCode) &&
                            p.getMainSN().equals(user.getUserSN()))
                    .map(p -> p.getTagSN())
                    .collect(Collectors.toList());
            String sql = "(";
            sql = sql + "receipterSN like '%" + user.getUserSN() + "%'";

            if (roles.size() > 0) {
                for (var roleSn : roles) {
                    sql = sql + "or receipterSN like '%" + roleSn + "%'";
                }
            }
            if (user.getDutySN() != null && !user.getDutySN().equals("")
                    && user.getDutySN().split(",").length > 0) {
                for (var dutySn : user.getDutySN().split(",")) {
                    sql = sql + "or receipterSN like '%" + dutySn + "%'";
                }
            }
            if (user.getOrganizationCode() != null &&
                    !user.getOrganizationCode().equals("") &&
                    user.getOrganizationCode().split(",").length > 0) {
                for (var organizationCode : user.getOrganizationCode().split(",")) {
                    sql = sql + "or receipterSN like '%" + organizationCode + "%'";
                }
            }
            sql = sql + ")";
            query.getQuery().put("userSql", sql);
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询站内信息收件信息", notes = "分页查询站内信息收件信息")
    @RequestMapping(value = "/queryMsgTypeCount", method = RequestMethod.GET)
    public WebResult queryMsgTypeCount() {
        try {
            PageQuery query = new PageQuery();
            var result = service.getTypeCount(query, 1000000, 0);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询站内信息收件信息", notes = "根据站内信息信息编号查询站内信息收件信息")
    @RequestMapping(value = "/msgSN", method = RequestMethod.GET)
    public WebResult findByMsgSn(
            @ApiParam(value = "站内信息编号", name = "msgSn", required = true)
            @RequestParam("msgSN") String msgSn) {
        return success("查询成功", service.findByMsgSN(msgSn));
    }

    @ApiOperation(value = "查询站内信息收件信息", notes = "根据用户编号查询站内信未读信息")
    @RequestMapping(value = "/{userSn}",
            method = RequestMethod.GET)
    public WebResult findByUserSn(@PathVariable String userSn) {
        try {
            var result = service.findByUserSn(userSn);
            if (result != null) {
                var data = result.stream().map(s -> s.getMsgSN()).toList();
                if (data.size() > 0) {
                    var datas = mainService.findList(data);
                    if (datas > 0) {
                        return success("查询成功", -1);
                    }
                }
                return success("查询成功", result.size());
            } else {
                return success("查询成功", 0);
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "保存站内信息收件信息", notes = "查询/更新站内信息收件信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(
            @ApiParam(value = "发送信息") @RequestBody MsgModel msgModel) {
        Integer result;
        try {
            result = service.saveSiteMessageReceipt(msgModel);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "收到站内信息收件信息信息", notes = "站内信息收件信息改为已读")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult receipt(
            @ApiParam(value = "站内信息信息编号", name = "msgSn",
                    allowMultiple = true, required = true)
            @RequestParam("msgSn") String[] msgSn,
            @ApiParam(value = "用户编号", name = "userSn", required = true)
            @RequestParam("userSn") String userSn) {
        try {
            if (service.receipt(msgSn, userSn) > 0) {
                return success("操作成功");
            } else {
                return fail("操作失败");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
}